package org.oschina.api;

import org.oschina.Constants;
import org.oschina.helper.cache.ICacheHelper;
import org.oschina.helper.cache.ICachePipeline;
import org.oschina.helper.messagequeue.IMessageQueueHelper;
import org.oschina.helper.query.BaseQuery;
import org.oschina.helper.query.IQueryHelper;
import org.oschina.queue.batchupdate.BatchUpdateBean;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseDAO extends BaseQuery {

	@Autowired
	private IQueryHelper queryHelper;
	@Autowired
	private ICacheHelper cacheHelper;
	@Autowired
	private IMessageQueueHelper messageQueueHelper;

	public ICacheHelper getCacheHelper() {
		return cacheHelper;
	}
	public IQueryHelper getQueryHelper() {
		return queryHelper;
	}
	public IMessageQueueHelper getMessageQueueHelper() {
		return messageQueueHelper;
	}
	
	protected boolean updateCache(String key,int id,String fields[],Object values[],int timeout)
	{
		String idKey=getKey(key,id);
		if(!getCacheHelper().exists(idKey))
			return false;
		
		ICachePipeline cachePipeline=getCacheHelper().getPipeline();
		for(int i=0;i<fields.length;i++)
			cachePipeline.hset(idKey, fields[i], values[i], timeout);
		cachePipeline.sync();
		return true;
	}
	
	/**
	 * 批量更新_队列
	 * @param sql
	 * @param args
	 * @return
	 */
	protected void batchUpdate_queue(String sql,Object args[])
	{
		messageQueueHelper.send(Constants.QUEUE_BATCH_UPDATE, new BatchUpdateBean(sql,args));
	}
}